#include "../include/mysqlite.h"
#include "../include/myhead.h"

sqlite3 *ppDb;

static int flags = 1; // 用于判断查找是否成功 0成功  1失败

sqlite3 *get_sqldb()
{
    return ppDb;
}

/**
 * 数据库初始化
 */
int mysqlite_init()
{
    int ret;
    if ((ret = sqlite3_open(SQLDBPATH, &ppDb)) != SQLITE_OK)
    {
        INFO_LOG("create table error: %s\n", sqlite3_errmsg(ppDb));
        return -1;
    }

    // 表是否存在，
    // CREATE TABLE stu(id int,name char,score int);
    // IF NOT EXISTS 表存在就直接使用，表不存在再创建
    char sqlbuf[256] = {0};
    char *errmsg = NULL;

    sprintf(sqlbuf, "CREATE TABLE IF NOT EXISTS login_users("
                        "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                        "name_id char, "
                        "passwd_ps char,"
                        "devnum char, "
                        "verify int UNIQUE);");

    if (sqlite3_exec(ppDb, sqlbuf, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        INFO_LOG("create table error: %s\n", errmsg);
        goto err;
    }
    sqlite3_free(errmsg);

    printf("sqlite_init success!\n");
    return 0;

err:
    sqlite3_free(errmsg);
    return -1;
}



#if 0
int sqladd_callback(void *arg, int ncolumn, char **f_value, char **f_name)
{
    int *val = arg;
    return *val == atoi(f_value[0]) ? 1 : 0;
}
#endif

/**
 * 向数据库中插入id ps devnum, 会根据id和ps的ascii相加进行验证,校验不存在添加，存在失败
 * 返回 0 添加成功  -1 添加失败
 */
int mysqlite_add(char *id, char *ps, char *devnum)
{
    if (id == NULL || ps == NULL)
    {
        ERR_PRIENT("input is NULL\n");
        return 1;
    }

    int verify = 0;
    char sqlbuf[128] = {0};
    char *errmsg = NULL;

    //计算用户名唯一性
    for (int i = 0; i < strlen(id); i++)
    {
        verify += id[i];
    }
    for (int i = 0; i < strlen(ps); i++)
    {
        verify += ps[i];
    }

    
#if 0  //查找verify是否存在，如果存在退出
    sprintf(sqlbuf, "SELECT verify FROM login_users WHERE verify=%d;", verify);

    if (sqlite3_exec(ppDb, sqlbuf, sqladd_callback, &verify, &errmsg) != SQLITE_OK)
    {
        INFO_LOG("SELECT table error: %s\n", errmsg);
        goto err;
    }
    sqlite3_free(errmsg);
#endif

    sprintf(sqlbuf, "INSERT INTO login_users(name_id,passwd_ps,devnum,verify) "
                            "VALUES ('%s', '%s', '%s', %d);", id, ps, devnum, verify);
    if (sqlite3_exec(ppDb, sqlbuf, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        INFO_LOG("create table error: [%s]\n", errmsg);
        goto err;
    }
    sqlite3_free(errmsg);
    INFO_LOG("INSERT success!\n");
    return 0;

err:
    sqlite3_free(errmsg);
    return -1;
}

int callback(void *arg, int ncolumn, char **f_value, char **f_name)
{
    char **buff = arg;
    for (int i = 0; i < ncolumn; i++)
    {
        if (strcmp(buff[i], f_value[i]) == 0)
        {
            flags = 0;
        }
        else
        {
            flags = 1;
        }
    }

    return flags;
}


/**
 * 按照id和ps查找数据库是否存在
 * 返回-1 参数为NULL， 0 匹配成功存在， 1 不存在
 */
int mysqlite_query_id_ps(char *id, char *ps)
{
    if (id == NULL || ps == NULL)
    {
        ERR_PRIENT("input is NULL\n");
        return -1;
    }

    char sqlbuf[128] = {0};
    char *errmsg = NULL;
    char *buff[2] = {id, ps};

    sprintf(sqlbuf, "SELECT name_id,passwd_ps FROM login_users "
                        "WHERE name_id='%s' and passwd_ps='%s';", id, ps);

    if (sqlite3_exec(ppDb, sqlbuf, callback, buff, &errmsg) != SQLITE_OK)
    {
        INFO_LOG("SELECT table error: %s\n", errmsg);
        goto err;
    }

    sqlite3_free(errmsg);
    if (flags == 0)
    {
        flags = 1;
        return 0;
    }
    else
    {
        flags = 1;
    }

err:
    sqlite3_free(errmsg);
    flags = 1;
    return 1;
}

void mysqlite_close()
{
    sqlite3_close(ppDb);
}
